package com.bl.chaosmtg;

import java.util.Random;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity {
	private static Button spin;
	private static TextView text;
	private static TextView extraText;
	private static String[] chaosList;
	private static String[] enchantWorldList;
	private static String[] personaList;
	private static String[] wackyList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		spin = (Button) this.findViewById(R.id.spin);
		text = (TextView) this.findViewById(R.id.text);
		extraText = (TextView) this.findViewById(R.id.extraText);
		Resources res = getResources();
		chaosList = res.getStringArray(R.array.chaosActions);
		enchantWorldList = res.getStringArray(R.array.enchantWorldActions);
		personaList = res.getStringArray(R.array.personaActions);
		wackyList = res.getStringArray(R.array.wackyActions);
		getFragmentManager();

		spin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				newRoll();
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private static void newRoll() {
		Random r = new Random();
		int pick = r.nextInt(chaosList.length);
		String result = chaosList[pick];
		String extra = "";
		int roll = 0;
		if (result.contains("1d6")) {
			result = result.replace("1d6", String.valueOf(r.nextInt(6) + 1));
		}
		if (result.contains("1d3")) {
			roll = r.nextInt() + 1;
			result = result.replace("1d3", String.valueOf(roll));
		}
		if (result.contains("1d10")) {
			result = result.replace("1d10", String.valueOf(r.nextInt(10) + 1));
		}
		if (pick == 99) {
			int key = r.nextInt(3);
			if (key == 0) {
				extra = enchantWorldList[r.nextInt(20)];
				// TODO: add handling for maintaining game state
			} else if (key == 1) {
				extra = personaList[r.nextInt(20)];
			} else if (key == 2) {
				extra = wackyList[r.nextInt(20)];
			}
		} else if (pick == 3 || pick == 187) {
			extra = enchantWorldList[r.nextInt(20)];
			// TODO: add handling for maintaining game state
		} else if (pick == 63 || pick == 135) {
			extra = enchantWorldList[r.nextInt(20)];
			// TODO: add handling for maintaining game state
			// adds to current enchantworlds
		} else if (pick == 158) {
			extra = enchantWorldList[r.nextInt(20)];
			extra += "\n" + enchantWorldList[r.nextInt(20)];
			extra += "\n" + enchantWorldList[r.nextInt(20)];
			// TODO: add handling for maintaining game state
			// destroy all EW add these three
		} else if (pick == 69 || pick == 103 || pick == 154 || pick == 172) {
			extra = personaList[r.nextInt(20)];
		} else if (pick == 70) {
			extra = "Opponent 1: ";
			extra += personaList[r.nextInt(20)] + "\n";
			for (int i = 2; i < 8; i++) {
				extra += "Opponent " + i + ": ";
				extra += personaList[r.nextInt(20)] + "\n";
			}
		} else if (pick == 49 || pick == 149) {
			extra = wackyList[r.nextInt(20)];
			extra += "\n";
			for (int i = 0; i < roll - 1; i++) {
				extra = wackyList[r.nextInt(20)];
				extra += "\n";
			}
		} else if (pick == 68 || pick == 87 || pick == 112 || pick == 176) {
			extra = wackyList[r.nextInt(20)];
		}
		text.setText(result);
		extraText.setMovementMethod(new ScrollingMovementMethod());
		extraText.setText(extra);

	}

	// for testing purposes
	@SuppressWarnings("unused")
	private static void newRoll(int pick) {
		Random r = new Random();
		String result = chaosList[pick];
		String extra = "";
		int roll = 0;
		if (result.contains("1d6")) {
			result = result.replace("1d6", String.valueOf(r.nextInt(6) + 1));
		}
		if (result.contains("1d3")) {
			roll = r.nextInt() + 1;
			result = result.replace("1d3", String.valueOf(roll));
		}
		if (result.contains("1d10")) {
			result = result.replace("1d10", String.valueOf(r.nextInt(10) + 1));
		}
		if (pick == 99) {
			int key = r.nextInt(3);
			if (key == 0) {
				extra = enchantWorldList[r.nextInt(20)];
			} else if (key == 1) {
				extra = personaList[r.nextInt(20)];
			} else if (key == 2) {
				extra = wackyList[r.nextInt(20)];
			}
		} else if (pick == 3 || pick == 187) {
			extra = enchantWorldList[r.nextInt(20)];
		} else if (pick == 63 || pick == 135) {
			extra = enchantWorldList[r.nextInt(20)];
		} else if (pick == 158) {
			extra = enchantWorldList[r.nextInt(20)];
			extra += "\n" + enchantWorldList[r.nextInt(20)];
			extra += "\n" + enchantWorldList[r.nextInt(20)];
		} else if (pick == 69 || pick == 103 || pick == 154 || pick == 172) {
			extra = personaList[r.nextInt(20)];
		} else if (pick == 70) {
			extra = "Opponent 1: ";
			extra += personaList[r.nextInt(20)] + "\n";
			for (int i = 2; i < 8; i++) {
				extra += "Opponent " + i + ": ";
				extra += personaList[r.nextInt(20)] + "\n";
			}
		} else if (pick == 49 || pick == 149) {
			extra = wackyList[r.nextInt(20)];
			extra += "\n";
			for (int i = 0; i < roll - 1; i++) {
				extra = wackyList[r.nextInt(20)];
				extra += "\n";
			}
		} else if (pick == 68 || pick == 87 || pick == 112 || pick == 176) {
			extra = wackyList[r.nextInt(20)];
		}
		text.setText(result);
		extraText.setMovementMethod(new ScrollingMovementMethod());
		extraText.setText(extra);

	}

}
